package MyWizard;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.netbeans.spi.wizard.Wizard;
import org.netbeans.spi.wizard.WizardPage;
import org.netbeans.spi.wizard.WizardPanelNavResult;
import preferences.Database.DBInterface;
import preferences.Database.DatabasePostgres;

public class Page0 extends WizardPage {

    private JComboBox dbms;
    private JTextField host;
    private JTextField port;
    private JTextField dbname;
    private JTextField username;
    private JPasswordField password;

    public Page0() {
        JPanel pnl = new JPanel();
        pnl.setLayout(new GridLayout(0, 2));

        pnl.add(new JLabel("DBMS:"));
        dbms = new JComboBox(new String[]{"PostgreSQL", "MySQL", "MonetDB", "JavaDB"});
        //dbms.setName("dbms");
        dbms.setSelectedIndex(0);
        pnl.add(dbms);


        pnl.add(new JLabel("Host:"));
        host = new JTextField();
        host.setText("localhost");
        //host.setName("host");
        pnl.add(host);

        pnl.add(new JLabel("Port:"));
        port = new JTextField();
        port.setText("5432");
        //port.setName("port");
        pnl.add(port);

        pnl.add(new JLabel("Database Name:"));
        dbname = new JTextField();
        dbname.setText("hy562");
        //dbname.setName("dbname");
        pnl.add(dbname);

        pnl.add(new JLabel("Username:"));
        username = new JTextField();
        username.setText("tsialiam");
        //username.setName("username");
        pnl.add(username);

        pnl.add(new JLabel("Password:"));
        password = new JPasswordField();
        //password.setText("20507");
        //password.setName("password");
        pnl.add(password);
        pnl.setPreferredSize(new Dimension(300, 150));
        add(pnl);
    }

    public static final String getDescription() {
        return "Database Settings";
    }

    @Override
    public WizardPanelNavResult allowNext(String stepName, Map settings, Wizard wizard) {
        DBInterface db = new DatabasePostgres(dbms.getSelectedItem().toString(), host.getText(), port.getText(), dbname.getText(), username.getText(), password.getPassword());
        String msg = null;
        if ((msg = db.init()) != null) {
            setProblem(msg);
            System.out.println(msg);
        }
        WizardDemo.db=db;
        return super.allowNext(stepName, settings, wizard);
    }

    @Override
    protected String validateContents(Component comp, Object o) {
        if (host.getText().length() == 0) {
            return "Enter the host";
        } else if (port.getText().length() == 0) {
            return "Enter the port";
        } else if (dbname.getText().length() == 0) {
            return "Enter the database name";
        } else if (username.getText().length() == 0) {
            return "Enter the username";
        } else {
            try {
                Integer.parseInt(port.getText());
                return null;
            } catch (NumberFormatException nfe) {
                return "Port should be an integer";
            }
        }
    }
}
